package rmhc.gui
{
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;

	public class ScrollableGrid extends GridContainer
	{
		protected var scrl:ScrollCalc;
		protected var bar:ScrollCalc;
		
		protected var slider:Sprite , ma:DisplayObject;
		
		protected var scrollEnabled:Boolean;
		
		public function ScrollableGrid(rowCount:uint=1, cellWidth:Number=80, cellHeight:Number=50, spaceX:Number=6, spaceY:Number=10)
		{
			super(rowCount, cellWidth, cellHeight, spaceX, spaceY);
			
			//cont.mask = getMaskArea();
		}
		
		public function initScroll():void
		{
			//ma = getMaskArea();
			//slider = getScrollSlider();
			//вычисляем ширину слайдера
			var visiblePart:Number = ma.width / cont.width;
			if ( visiblePart >= 1 )
			{
				enableScroll( false );
			}
			else
			{
				enableScroll( true );
				slider.width = ma.width * visiblePart;
			}
			
			//расчёты
			bar = new ScrollCalc( ma.x , ma.x + ma.width - slider.width );
			scrl = new ScrollCalc( cont.x , cont.x - cont.width + ma.width );
		}
		
		public function dragSlider( drag:Boolean ):void
		{
			if ( drag )
			{
				var r:Rectangle = new Rectangle( bar.minimum , slider.y , bar.maximum - bar.minimum , 0 );
				slider.startDrag( false , r );
			}
			else
			{
				slider.stopDrag();
			}
		}
		
		public function updateSliderPos( e:Event ):void
		{
			bar.value = slider.x;
			scrl.pos = bar.pos;
			updateCont( true );
		}
		public function setScrollPos( xpos:Number ):void
		{
			scrl.pos = xpos;
			bar.pos = xpos;
			updateCont();
		}
		public function getScrollPos():Number
		{
			return scrl.pos;
		}
		/*public function scrollLeft( n:Number = 1 ):void
		{
			scrl.value += cellWidth + spaceX;
			bar.pos = scrl.pos;
			updateCont();
		}
		public function scrollRight( n:Number = 1 ):void
		{
			scrl.value -= cellWidth + spaceX;
			bar.pos = scrl.pos;
			updateCont();
		}*/
		public function scrollOnce( n:Number = 1 ):void
		{
			scrl.value -= n * ( cellWidth + spaceX );
			bar.pos = scrl.pos;
			updateCont();
		}
		
		protected function updateCont( immediate:Boolean = false ):void
		{
			if ( slider.x != bar.value )
			{
				if ( immediate )
				{
					slider.x = bar.value;
				}
				else
				{
					TweenLite.to( slider , 0.2 , { x : bar.value } );
				}
			}
			if ( cont.x != scrl.value )
			{
				if ( immediate )
				{
					cont.x = scrl.value;
				}
				else
				{
					TweenLite.to( cont , 0.2 , { x : scrl.value } );
				}
			}
		}
		
		public function setMaskArea( o:DisplayObject ):void
		{
			ma = o;
			cont.mask = ma;
		}
		public function setScrollSlider( s:Sprite ):void
		{
			slider = s;
		}
		protected function enableScroll( enabled:Boolean ):void
		{
			scrollEnabled = enabled;
		}
		public function isEnabled():Boolean
		{
			return scrollEnabled;
		}
		/*public function getBtnLeft():DisplayObject
		{
			return null;
		}
		public function getBtnRight():DisplayObject
		{
			return null;
		}*/
	}
}